import axios from "axios";
import {
	ElMessage
} from 'element-plus'
import { ElLoading } from 'element-plus'
export default function request(config) {
	const instance = axios.create({
		// baseURL:"",
		timeout: 20000
	})
	//请求拦截
	// ElLoading.service()
	instance.interceptors.request.use(config => {
		const token = window.localStorage.getItem('token');
		if (token) {
			config.headers.token = token;
			config.headers.Authorization = 'Bearer ' + token;
		}
		//数据加载动画
	 ElLoading.service()
	
		return config;
	}, err => {
		ElMessage({
			message: '服务器繁忙，请稍后再试...',
			type: 'warning'
		})
		return Promise.reject(err)
	})
	//响应拦截
	instance.interceptors.response.use(res => {
		//关闭loading
		ElLoading.service().close()
		if (res.status >= 200 && res.status < 300) {
			//解构出数据
			return res;
		} else {
			//创建错误对象
			const err = new Error('服务器繁忙，稍后重试');
			err.reason = res;
			ElMessage({
				message: '服务器繁忙，稍后重试',
				type: 'warning'
			})
			//停止其他操作
			return Promise.reject(err)
		}

	})
	return instance(config);
}
